package com.hbpu.encap.socket.tcp;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPFileCopyServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("服务已创建，等待连接....");
        Socket socket = serverSocket.accept();
        System.out.println("连接成功！");

        InputStream inputStream = socket.getInputStream();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        BufferedOutputStream fileOutputStream = new BufferedOutputStream(new FileOutputStream("E:\\Update\\上传图片.png"));
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = bufferedInputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes, 0, len);
        }
        fileOutputStream.close();

        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("图片已成功上传至服务器！");
        bufferedWriter.newLine();
        bufferedWriter.flush();

        bufferedWriter.close();

        bufferedInputStream.close();
        socket.close();
    }
}
